package com.shushan.home;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.home.adapter.AppUseTimeAdapter;
import com.shushan.home.adapter.StudyDetailAdapter;
import com.shushan.home.adapter.TotalUseTimeAdapter;
import com.shushan.home.bean.RecordDetailBean;
import com.shushan.home.bean.StatisticsBean;
import com.shushan.home.bean.TotalPlayBean;
import com.shushan.mixedexam.OralCalculateScoreActivity;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetListCommListener;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.pages.NewRecordResultActivity;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.ui.OffsetRecyclerDivider;
import com.shushan.util.DateUtils;
import com.shushan.util.DensityUtil;
import com.shushan.util.ToolUtils;

import java.util.List;

/**
 * @ClassName: StudyStatisticsActivity
 * @Desciption: //学习统计Activity
 * @author: zhangshihao
 * @date: 2018-09-19
 */
public class StudyStatisticsActivity extends BaseActivity implements View.OnClickListener,StudyDetailAdapter.OnTitleBarClickListener{

    private static final String TAG = StudyStatisticsActivity.class.getSimpleName();

    private RecyclerView recyclerView;
    private ImageView ivBack;
    private TextView tvTitle,tvDate;
//    private TextView tvPlayTime;
//    private RecyclerView rvTotalUseTime;
//    private RelativeLayout rlTotalPlayTime;
    private LinearLayout llEmptyState;

    private int position = -1;
    private BabyApplication babyApplication = BabyApplication.getInstance();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_study_statistics);

        ScreenManager sm = new ScreenManager();
        if(!sm.setDeepStatusBar(true,this)){
            findViewById(R.id.view_header).setVisibility(View.GONE);
        }

        position = getIntent().getIntExtra("position",-1);

        initView();

        getData();
    }

    private void initView(){
        recyclerView = findViewById(R.id.rv_study_detail);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new OffsetRecyclerDivider(DensityUtil.dip2px(this,8f)));
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        tvTitle = findViewById(R.id.tv_title);
        tvDate = findViewById(R.id.tv_date);
        if(position >= 0){
            tvTitle.setText(getTitleStr());
            tvDate.setVisibility(View.GONE);
        }else{
            tvDate.setText(DateUtils.getNowDate("yyyy-MM-dd"));
        }
//        tvPlayTime = findViewById(R.id.tv_play_time);
//        rvTotalUseTime = findViewById(R.id.rv_app_use_time);
//        rvTotalUseTime.setLayoutManager(new LinearLayoutManager(this));
//        rlTotalPlayTime = findViewById(R.id.rl_total_play_time);
//        if(position != -1){//非详情跳转过来的
//            rlTotalPlayTime.setVisibility(View.GONE);
//        }
        llEmptyState = findViewById(R.id.ll_empty_state);
    }

    private void getData(){
        int childId = babyApplication.getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.RECORD_DETAIL);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("child_id",childId)
                .putValue("practice_type",getPracticeType());
        showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<StatisticsBean>() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(StatisticsBean statisticsBean) {
                        setData(statisticsBean);
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    private void setData(StatisticsBean statisticsBean){
        if(statisticsBean == null){
            showToast("数据为空");
            return;
        }
        if(ToolUtils.isListEmpty(statisticsBean.getRecordDetailBeans())/* && statisticsBean.totalPlayBean.time == 0*/) {
            recyclerView.setVisibility(View.GONE);
            llEmptyState.setVisibility(View.VISIBLE);
        }else{
            recyclerView.setVisibility(View.VISIBLE);
            llEmptyState.setVisibility(View.GONE);
            StudyDetailAdapter adapter = new StudyDetailAdapter(this, statisticsBean.getRecordDetailBeans(), position);
            adapter.setOnTitleBarClickListener(this);
            recyclerView.setAdapter(adapter);
//            TotalPlayBean totalPlayBean = statisticsBean.totalPlayBean;
//            tvPlayTime.setText("获得" + totalPlayBean.time + "分钟");
//            rvTotalUseTime.setAdapter(new TotalUseTimeAdapter(totalPlayBean.getTotalPlayListBeans(), totalPlayBean.time));
        }
    }

    private String getPracticeType(){
        String practiceType = "";
        switch (position){
            case 0:
                practiceType = String.valueOf(Constants.TYPE_SYNC_EXERCISE);
                break;
            case 1:
                practiceType = String.valueOf(Constants.TYPE_STRANGE_WORDS_EXERCISE);
                break;
            case 2:
                practiceType = String.valueOf(Constants.TYPE_SPEAKING_EXERCISE);
                break;
            case 3:
                practiceType = String.valueOf(Constants.TYPE_ENGLISH_WORDS_EXERCISE);
                break;
            case 4:
                practiceType = String.valueOf(Constants.TYPE_HEARING_EXERCISE);
                break;
            case 5:
                practiceType = String.valueOf(Constants.TYPE_ORAL_CALCULATE_EXERCISE);
                break;
            case 6:
                practiceType = String.valueOf(Constants.TYPE_SMART_EXERCISE);
                break;
            case 7:
                practiceType = String.valueOf(Constants.TYPE_CHINESE_SECT_EXERCISE);
                break;
            case 8:
                practiceType = String.valueOf(Constants.TYPE_MACH_SECT_EXERCISE);
                break;
            case 9:
                practiceType = String.valueOf(Constants.TYPE_ENGLISH_SECT_EXERCISE);
                break;
            default:
                break;
        }
        return practiceType;
    }

    private String getTitleStr(){
        String title = "";
        switch (position){
            case 0:
                title = "同步练习记录";
                break;
            case 1:
                title = "生字练习记录";
                break;
            case 2:
                title = "口语练习记录";
                break;
            case 3:
                title = "单词练习记录";
                break;
            case 4:
                title = "听力练习记录";
                break;
            case 5:
                title = "口算练习记录";
                break;
            case 6:
                title = "智能练习记录";
                break;
            case 7:
                title = "语文分科练习记录";
                break;
            case 8:
                title = "数学分科练习记录";
                break;
            case 9:
                title = "英语分科练习记录";
                break;
        }
        return title;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                onBackAction();
                break;
        }
    }

    @Override
    public void onTitleBarClick(int position,int paperId,int practiceType) {
        if(practiceType == Constants.TYPE_ORAL_CALCULATE_EXERCISE) {
            Intent intent = new Intent(this,OralCalculateScoreActivity.class);
            intent.putExtra("paper_id", paperId);
            intent.putExtra("fromRecord",true);
            startActivityAllow(intent);
        }else {
            Intent intent = new Intent(this, NewRecordResultActivity.class);
            intent.putExtra("paper_id", paperId);
            intent.putExtra("practice_type", practiceType);
            startActivityAllow(intent);
        }
    }

}
